﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;
using Newtonsoft.Json.Linq;
using System.Web;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using System.Diagnostics;

namespace MTA.TripPlanner
{
    public class JSONRequest
    {
        private FeedUrlBuilder _pars = null;
        private String _feedurl = null;

        public FeedUrlBuilder Params
        {
            get
            {
                if (_pars == null)
                    _pars = new FeedUrlBuilder(_feedurl);
                return _pars;
            }
            set
            {
                _pars = value;
            }
        }

        public JSONRequest(String feedurl)
        {
            _feedurl = feedurl;
        }

        public JObject MakeRequest()
        {
            return MakeRequest(_feedurl, _pars);
        }

        public static JObject MakeRequest(String feedurl)
        {
            return MakeRequest(feedurl, null);
        }

        public static JObject MakeRequest(String pageName, FeedUrlBuilder pars)
        {
            String _url = pars.GetUrl(); // attac parameters           

            Debug.Print(_url);
            HttpWebRequest webRequest = WebRequest.Create(_url) as HttpWebRequest;
            HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse;
            StreamReader textreader = new StreamReader(response.GetResponseStream());
   
            JsonReader reader = new JsonTextReader(textreader);
            JObject data = JObject.Load(reader);
          
            
            return data;
        }

 
    }
}
